
package com.fire.user.service.jkservice;

import com.fire.user.util.HttpUtils;
import com.fire.user.util.XmlMessageUtil;
import junit.framework.Assert;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fire.user.model.jkmodel.ReqDTO;
import com.fire.user.model.jkmodel.SmsInfoDTO;
import com.fire.user.model.jkmodel.XmlMessageDTO;



/**
 * 类描述：
 * 
 * @author: Aspire
 * @version 1.0
 */
@Service
public class SmsService {
    private static Logger logger = Logger.getLogger(SmsService.class);
    // 后续封装在配置表
    private static String PARTNER_CODE = "YiBaoTong";
    private static String URL = "https://pfe.dev.cmrh.com:8080/pfe/connect";
    private final static String MD5_KEY = "0506e862a9f6f3207a901e0b673ce9de";

    private static String SUCCESS_CODE = "0000";

    /**
     * 调金科短信验证码接口
     */
    public void sendSms() {
        String requestId = "";

        ReqDTO<SmsInfoDTO> reqDTO = new ReqDTO<>();
        reqDTO.setTransCode("sendMsm");
        reqDTO.setPartnerCode(PARTNER_CODE);
        reqDTO.setRequestId(requestId);
        reqDTO.setTimestamp(System.currentTimeMillis());
        reqDTO.setReqData(getSmsInfoDTO());
        String responseStr = postRequest(reqDTO);
        XmlMessageDTO xmlMessageDTO = XmlMessageUtil.parseXml(responseStr);
        Assert.assertTrue(checkSign(xmlMessageDTO));
        JSONObject resultObj = JSON.parseObject(xmlMessageDTO.getContent());
        Assert.assertEquals(SUCCESS_CODE, resultObj.getString("resCode"));
        System.out.println(JSON.toJSONString(xmlMessageDTO.getContent()));
    }

    /**
     * 构造短信验证码请求结构
     * 
     * @return
     */
    private SmsInfoDTO getSmsInfoDTO() {
        SmsInfoDTO smsInfo = new SmsInfoDTO();
        smsInfo.setMobileNo("15018527400");
        smsInfo.setSmsCode("888888");
        return smsInfo;
    }

    /**
     * 检查签名是否合法
     * 
     * @param xmlMessageDTO
     * @return
     */
    private boolean checkSign(XmlMessageDTO xmlMessageDTO) {
        String sign = DigestUtils.md5Hex(MD5_KEY + xmlMessageDTO.getContent());
        if (sign.equals(xmlMessageDTO.getSign())) {
            return true;
        }
        return false;
    }

    /**
     * 提交请求
     * 
     * @param reqDTO
     *            请求信息
     * @return 响应内容
     */
    private String postRequest(ReqDTO<?> reqDTO) {
        String content = JSON.toJSONString(reqDTO);
        String sign = DigestUtils.md5Hex(MD5_KEY + content);
        String postXmlStr = XmlMessageUtil.createXml(PARTNER_CODE, sign, content);
        System.out.println("请求的消息1" + postXmlStr);
        String responseStr = null;
        try {
            responseStr = HttpUtils.request(postXmlStr, URL, 180 * 1000);
            System.out.println("返回消息" + responseStr);
            System.out.println(responseStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseStr;
    }

    public static void main(String[] args) {
        SmsService service = new SmsService();
        service.sendSms();
    }

}
